{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w= 0.0\n",
      "\t 1 2 0.0 4.0\n",
      "\t 2 4 0.0 16.0\n",
      "\t 3 6 0.0 36.0\n",
      "MSE= 18.666666666666668\n",
      "w= 0.1\n",
      "\t 1 2 0.1 3.9601\n",
      "\t 2 4 0.2 15.8404\n",
      "\t 3 6 0.30000000000000004 35.640899999999995\n",
      "MSE= 18.480466666666665\n",
      "w= 0.2\n",
      "\t 1 2 0.2 3.8415999999999997\n",
      "\t 2 4 0.4 15.366399999999999\n",
      "\t 3 6 0.6000000000000001 34.5744\n",
      "MSE= 17.927466666666664\n",
      "w= 0.30000000000000004\n",
      "\t 1 2 0.30000000000000004 3.6481\n",
      "\t 2 4 0.6000000000000001 14.5924\n",
      "\t 3 6 0.9000000000000001 32.832899999999995\n",
      "MSE= 17.024466666666665\n",
      "w= 0.4\n",
      "\t 1 2 0.4 3.3855999999999993\n",
      "\t 2 4 0.8 13.542399999999997\n",
      "\t 3 6 1.2000000000000002 30.470399999999994\n",
      "MSE= 15.799466666666666\n",
      "w= 0.5\n",
      "\t 1 2 0.5 3.0625\n",
      "\t 2 4 1.0 12.25\n",
      "\t 3 6 1.5 27.5625\n",
      "MSE= 14.291666666666666\n",
      "w= 0.6000000000000001\n",
      "\t 1 2 0.6000000000000001 2.6895999999999995\n",
      "\t 2 4 1.2000000000000002 10.758399999999998\n",
      "\t 3 6 1.8000000000000003 24.2064\n",
      "MSE= 12.551466666666665\n",
      "w= 0.7000000000000001\n",
      "\t 1 2 0.7000000000000001 2.2800999999999996\n",
      "\t 2 4 1.4000000000000001 9.120399999999998\n",
      "\t 3 6 2.1 20.520899999999994\n",
      "MSE= 10.640466666666663\n",
      "w= 0.8\n",
      "\t 1 2 0.8 1.8495999999999997\n",
      "\t 2 4 1.6 7.398399999999999\n",
      "\t 3 6 2.4000000000000004 16.6464\n",
      "MSE= 8.631466666666666\n",
      "w= 0.9\n",
      "\t 1 2 0.9 1.4161\n",
      "\t 2 4 1.8 5.6644\n",
      "\t 3 6 2.7 12.7449\n",
      "MSE= 6.608466666666666\n",
      "w= 1.0\n",
      "\t 1 2 1.0 1.0\n",
      "\t 2 4 2.0 4.0\n",
      "\t 3 6 3.0 9.0\n",
      "MSE= 4.666666666666667\n",
      "w= 1.1\n",
      "\t 1 2 1.1 0.6240999999999997\n",
      "\t 2 4 2.2 2.4963999999999986\n",
      "\t 3 6 3.3000000000000003 5.616899999999997\n",
      "MSE= 2.9124666666666648\n",
      "w= 1.2000000000000002\n",
      "\t 1 2 1.2000000000000002 0.31359999999999955\n",
      "\t 2 4 2.4000000000000004 1.2543999999999982\n",
      "\t 3 6 3.6000000000000005 2.822399999999996\n",
      "MSE= 1.4634666666666647\n",
      "w= 1.3\n",
      "\t 1 2 1.3 0.0960999999999999\n",
      "\t 2 4 2.6 0.3843999999999996\n",
      "\t 3 6 3.9000000000000004 0.8648999999999994\n",
      "MSE= 0.4484666666666663\n",
      "w= 1.4000000000000001\n",
      "\t 1 2 1.4000000000000001 0.0015999999999999673\n",
      "\t 2 4 2.8000000000000003 0.006399999999999869\n",
      "\t 3 6 4.2 0.014399999999999812\n",
      "MSE= 0.0074666666666665495\n",
      "w= 1.5\n",
      "\t 1 2 1.5 0.0625\n",
      "\t 2 4 3.0 0.25\n",
      "\t 3 6 4.5 0.5625\n",
      "MSE= 0.2916666666666667\n",
      "w= 1.6\n",
      "\t 1 2 1.6 0.31360000000000054\n",
      "\t 2 4 3.2 1.2544000000000022\n",
      "\t 3 6 4.800000000000001 2.822400000000005\n",
      "MSE= 1.4634666666666691\n",
      "w= 1.7000000000000002\n",
      "\t 1 2 1.7000000000000002 0.792100000000001\n",
      "\t 2 4 3.4000000000000004 3.168400000000004\n",
      "\t 3 6 5.1000000000000005 7.128900000000009\n",
      "MSE= 3.6964666666666712\n",
      "w= 1.8\n",
      "\t 1 2 1.8 1.5376000000000005\n",
      "\t 2 4 3.6 6.150400000000002\n",
      "\t 3 6 5.4 13.838400000000005\n",
      "MSE= 7.1754666666666695\n",
      "w= 1.9000000000000001\n",
      "\t 1 2 1.9000000000000001 2.592100000000001\n",
      "\t 2 4 3.8000000000000003 10.368400000000005\n",
      "\t 3 6 5.7 23.32890000000002\n",
      "MSE= 12.096466666666677\n",
      "w= 2.0\n",
      "\t 1 2 2.0 4.0\n",
      "\t 2 4 4.0 16.0\n",
      "\t 3 6 6.0 36.0\n",
      "MSE= 18.666666666666668\n",
      "w= 2.1\n",
      "\t 1 2 2.1 5.8081000000000005\n",
      "\t 2 4 4.2 23.232400000000002\n",
      "\t 3 6 6.300000000000001 52.27290000000003\n",
      "MSE= 27.104466666666678\n",
      "w= 2.2\n",
      "\t 1 2 2.2 8.065600000000003\n",
      "\t 2 4 4.4 32.262400000000014\n",
      "\t 3 6 6.6000000000000005 72.59040000000006\n",
      "MSE= 37.63946666666669\n",
      "w= 2.3000000000000003\n",
      "\t 1 2 2.3000000000000003 10.824100000000007\n",
      "\t 2 4 4.6000000000000005 43.29640000000003\n",
      "\t 3 6 6.9 97.41690000000006\n",
      "MSE= 50.512466666666704\n",
      "w= 2.4000000000000004\n",
      "\t 1 2 2.4000000000000004 14.137600000000011\n",
      "\t 2 4 4.800000000000001 56.550400000000046\n",
      "\t 3 6 7.200000000000001 127.23840000000011\n",
      "MSE= 65.97546666666672\n",
      "w= 2.5\n",
      "\t 1 2 2.5 18.0625\n",
      "\t 2 4 5.0 72.25\n",
      "\t 3 6 7.5 162.5625\n",
      "MSE= 84.29166666666667\n",
      "w= 2.6\n",
      "\t 1 2 2.6 22.657600000000006\n",
      "\t 2 4 5.2 90.63040000000002\n",
      "\t 3 6 7.800000000000001 203.91840000000002\n",
      "MSE= 105.73546666666668\n",
      "w= 2.7\n",
      "\t 1 2 2.7 27.98410000000001\n",
      "\t 2 4 5.4 111.93640000000003\n",
      "\t 3 6 8.100000000000001 251.85690000000014\n",
      "MSE= 130.59246666666672\n",
      "w= 2.8000000000000003\n",
      "\t 1 2 2.8000000000000003 34.10560000000002\n",
      "\t 2 4 5.6000000000000005 136.42240000000007\n",
      "\t 3 6 8.4 306.9504000000001\n",
      "MSE= 159.15946666666673\n",
      "w= 2.9000000000000004\n",
      "\t 1 2 2.9000000000000004 41.088100000000026\n",
      "\t 2 4 5.800000000000001 164.3524000000001\n",
      "\t 3 6 8.700000000000001 369.7929000000003\n",
      "MSE= 191.7444666666668\n",
      "w= 3.0\n",
      "\t 1 2 3.0 49.0\n",
      "\t 2 4 6.0 196.0\n",
      "\t 3 6 9.0 441.0\n",
      "MSE= 228.66666666666666\n",
      "w= 3.1\n",
      "\t 1 2 3.1 57.91210000000002\n",
      "\t 2 4 6.2 231.64840000000007\n",
      "\t 3 6 9.3 521.2089000000001\n",
      "MSE= 270.2564666666667\n",
      "w= 3.2\n",
      "\t 1 2 3.2 67.89760000000004\n",
      "\t 2 4 6.4 271.59040000000016\n",
      "\t 3 6 9.600000000000001 611.0784000000003\n",
      "MSE= 316.8554666666668\n",
      "w= 3.3000000000000003\n",
      "\t 1 2 3.3000000000000003 79.03210000000004\n",
      "\t 2 4 6.6000000000000005 316.12840000000017\n",
      "\t 3 6 9.9 711.2889000000001\n",
      "MSE= 368.8164666666668\n",
      "w= 3.4000000000000004\n",
      "\t 1 2 3.4000000000000004 91.39360000000005\n",
      "\t 2 4 6.800000000000001 365.5744000000002\n",
      "\t 3 6 10.200000000000001 822.5424000000004\n",
      "MSE= 426.50346666666684\n",
      "w= 3.5\n",
      "\t 1 2 3.5 105.0625\n",
      "\t 2 4 7.0 420.25\n",
      "\t 3 6 10.5 945.5625\n",
      "MSE= 490.2916666666667\n",
      "w= 3.6\n",
      "\t 1 2 3.6 120.12160000000002\n",
      "\t 2 4 7.2 480.48640000000006\n",
      "\t 3 6 10.8 1081.0944000000002\n",
      "MSE= 560.5674666666667\n",
      "w= 3.7\n",
      "\t 1 2 3.7 136.65610000000004\n",
      "\t 2 4 7.4 546.6244000000002\n",
      "\t 3 6 11.100000000000001 1229.9049000000005\n",
      "MSE= 637.7284666666669\n",
      "w= 3.8000000000000003\n",
      "\t 1 2 3.8000000000000003 154.75360000000003\n",
      "\t 2 4 7.6000000000000005 619.0144000000001\n",
      "\t 3 6 11.4 1392.7824000000005\n",
      "MSE= 722.1834666666668\n",
      "w= 3.9000000000000004\n",
      "\t 1 2 3.9000000000000004 174.50410000000008\n",
      "\t 2 4 7.800000000000001 698.0164000000003\n",
      "\t 3 6 11.700000000000001 1570.5369000000007\n",
      "MSE= 814.352466666667\n",
      "w= 4.0\n",
      "\t 1 2 4.0 196.0\n",
      "\t 2 4 8.0 784.0\n",
      "\t 3 6 12.0 1764.0\n",
      "MSE= 914.6666666666666\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x_data = [1,2,3]\n",
    "y_data = [2,4,6]\n",
    "\n",
    "def forward(x):\n",
    "    return x*w\n",
    "\n",
    "def loss(x,y):\n",
    "    y_pred = forward(x)\n",
    "    return (y_pred-y)**2\n",
    "\n",
    "w_list =[]\n",
    "mse_list = []\n",
    "\n",
    "for w in np.arange(0.0,4.1,0.1):\n",
    "    print('w=',w)\n",
    "    l_sum = 0\n",
    "    for x_val,y_val in zip(x_data,y_data):\n",
    "        y_pred_val = forward(x_val)\n",
    "        loss_val = loss(y_pred_val,y_val)\n",
    "        l_sum += loss_val\n",
    "        print('\\t',x_val,y_val,y_pred_val,loss_val)\n",
    "    print('MSE=',l_sum/3)\n",
    "    w_list.append(w)\n",
    "    mse_list.append(l_sum/3)\n",
    "\n",
    "plt.plot(w_list,mse_list)\n",
    "plt.ylabel('loss')\n",
    "plt.xlabel('w')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
